<?php
/**
 * The session object.
 */
class Session {
    /**
     * Nest separator.
     * @var string
     */
	protected $nestSeparator = '.';
    /**
     * The namespace to use.
     * @var string
     */
	protected $namespace = 'default';

    /**
     * The constructor. Start the session and set up the default namespace.
     */
	public function __construct() {
		session_start();
		if(!isset($_SESSION[$this->namespace])) {
			$_SESSION[$this->namespace] = array();
		}
	}

    /**
     * Merge an array into the session.
     * @param array $arr
     */
	public function mergeArray($arr) {
		$_SESSION[$this->namespace] = array_merge($_SESSION[$this->namespace], $arr);
	}

    /**
     * Set a value in the session.
     * @param string $key
     * @param mixed $value
     */
	public function setValue($key, $value) {
		$keyparts = explode($this->nestSeparator, $key);
		$temp = &$_SESSION[$this->namespace];
		for($i = 0; $i < count($keyparts) - 1; $i++) {
			if(!is_array($temp[$keyparts[$i]])) {
				$temp[$keyparts[$i]] = array();
			}
			$temp = &$temp[$keyparts[$i]];
		}
		$temp[end($keyparts)] = $value;
	}

    /**
     * Get a value from the session.
     * @return mixed
     */
	public function getValue($key) {
		$keyparts = explode($this->nestSeparator, $key);
		$temp = $_SESSION[$this->namespace];
		foreach($keyparts as $keypart) {
			$temp = $temp[$keypart];
		}
		return $temp;
	}

    /**
     * Unset the whole namespace.
     */
	public function unsetNamespace() {
		$_SESSION[$this->namespace] = array();
	}

    /**
     * Unset the entire session.
     */
	public function unsetAll() {
		$_SESSION = array();
	}

    /**
     * Get the namespace of this session.
     * @return string
     */
    public function getNamespace() {
        return $this->namespace;
    }
}
?>